package com.sls.web.manager.recharge.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.MatchSupplier;
import com.sls.web.manager.recharge.pojo.Recharge;
import com.sls.web.manager.recharge.service.PhoneRechargeService;
import com.sls.web.manager.socket.SuperService;

@Service("rechargeService")
public class PhoneRechargeServiceImpl extends SuperService implements PhoneRechargeService {

	@Override
	public Boolean recharge(Recharge recharge, UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		// 测试用。。杭州
		head.setUserAreaCode("330100");
		// 测试用。。支付账号
		head.setUserAccount("10959");
		// 测试用。。杭州西湖代理商特价模板
		head.setPriceTemplateId("f6943119-3c42-42fb-ace3-fa22b29da4d6");
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.PHONE_RECHARGE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, recharge);
		if (TransmissionCode.RECHARGE_NO_LOCALE_FOUND_ERROR.getCode().equals(dto.getHead().getResponseCode())) {
			return false;
		} else if (dto.getBody() != null) {
			Gson gson = CommonUtil.gson();
			throw new ManagerException(gson.fromJson(dto.getBody(), String.class));
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<MatchSupplier> viewRecharge(Recharge recharge, UserInfoVO userInfoVO) throws Exception {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.PHONE_RECHARGE_LIST);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, recharge);
		if (!TransmissionCode.SUCCESS.getCode().equals(dto.getHead().getResponseCode())) {
			throw new ManagerException(dto.getHead().getResponseMessage());
		} else {
			List<MatchSupplier> saList = gson.fromJson(dto.getBody(), new TypeToken<List<MatchSupplier>>() {
			}.getType());
			return saList;
		}
	}
}
